<?php

require_once $_SERVER['DOCUMENT_ROOT'] . '/../../Configuration/ambiente.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/../../Configuration/DbConfig.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/../../common/common.php';

$smarty->assign('obterXML', 'ko');
$smarty->assign('descodificar', 'ko');
$smarty->assign('escreverBD', 'ko');
$smarty->assign('updateCidadesTuga', 'ko');
$smarty->assign('verificaTabelaDePaises', 'ko');
$smarty->assign('verificaTabelaDeBancos', 'ko');
$smarty->assign('verificaTabelaDeBancos', 'ko');
$smarty->assign('inserircambios', 'ko');
$smarty->assign('verificaTabelaDeBancosPagadores', 'ko');

/* obterXML(); */

function obterXML() {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    $result = $russlavBank->getFullDb(); //ok

    if ($russlavBank->testa($result)) {
        if ($russlavBank->escreveXML($result)) {
            $time_end = microtime(true);
            $time = $time_end - $time_start;
            $smarty->assign('obterXML', 'ok');
            $smarty->assign('timeobterXML', $time);
        } else {
            $smarty->assign('obterXML', 'ko');
        }
    }
}

function obterXMLUpdate($versao) {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    $result = $russlavBank->getUpdatesForDb($versao); //ok
    if ($russlavBank->testa($result)) {
        if ($russlavBank->escreveXML($result)) {
            $time_end = microtime(true);
            $time = $time_end - $time_start;
            $smarty->assign('obterXMLUpdate', 'ok');
            $smarty->assign('timeobterXMLUpdate', $time);
        } else {
            $smarty->assign('obterXMLUpdate', 'ko');
        }
    }
}

function descodificar() {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    if ($russlavBank->readXMLCodificado()) {
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        $smarty->assign('descodificar', 'ok');
        $smarty->assign('timedescodificar', $time);
    } else {
        $smarty->assign('descodificar', 'ko');
    }
}

function escreverBD() {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    if ($russlavBank->escreveNaBD()) {
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        $smarty->assign('escreverBD', 'ok');
        $smarty->assign('timeescreverBD', $time);
    } else {
        $smarty->assign('escreverBD', 'ko');
    }
}

function updateCidadesTuga() {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    if ($russlavBank->updateCidadesTuga()) {
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        $smarty->assign('updateCidadesTuga', 'ok');
        $smarty->assign('timeupdateCidadesTuga', $time);
    } else {
        $smarty->assign('timeupdateCidadesTuga', 'ko');
    }
}

function verificaTabelaDePaises() {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    if ($russlavBank->verificaTabelaDePaises()) {
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        $smarty->assign('verificaTabelaDePaises', 'ok');
        $smarty->assign('timeverificaTabelaDePaises', $time);
    } else {
        $smarty->assign('timeverificaTabelaDePaises', 'ko');
    }
}

function getBancos() {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    if ($russlavBank->getBancos()) {
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        $smarty->assign('verificaTabelaDeBancos', 'ok');
        $smarty->assign('timeverificaTabelaDeBancos', $time);
    } else {
        $smarty->assign('timeverificaTabelaDeBancos', 'ko');
    }
}

function actualizaPagadores() {
    global $smarty, $russlavBank;
    $time_start = microtime(true);
    if ($russlavBank->actualizaPagadores()) {
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        $smarty->assign('verificaTabelaDeBancosPagadores', 'ok');
        $smarty->assign('timeverificaTabelaDeBancosPagadores', $time);
    } else {
        $smarty->assign('timeverificaTabelaDeBancosPagadores', 'ko');
    }
}

function inserirCambios() {

    global $smarty, $russlavBank;
    $time_start = microtime(true);
    if ($russlavBank->inserirCambios()) {
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        $smarty->assign('inserircambios', 'ok');
        $smarty->assign('timeinserircambios', $time);
    } else {
        $smarty->assign('timeinserircambios', 'ko');
    }
}

$accao = isset($_POST['accao']) ? $_POST['accao'] : null;

switch ($accao) {
    case "obterXML":obterXML();
        break;
    case 'verificaTabelaDePaises':verificaTabelaDePaises();
        break;
    case "descodificar":descodificar();
        break;
    case "escreverBD":escreverBD();
        break;
    case "updateCidadesTuga":updateCidadesTuga();
        break;
    case "verificaTabelaDeBancos":getBancos();
        break;
    case "verificaTabelaDeBancosPagadores":actualizaPagadores();
        break;
    case "inserircambios":inserirCambios();
        break;
}
$smarty->display('index.tpl');